package com.ylyq.ylxx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ylyq.ylxx.entity.Chat;
import com.ylyq.ylxx.mapper.ChatMapper;
import com.ylyq.ylxx.model.bean.ChatData;
import com.ylyq.ylxx.model.bean.GetOrderDetail;
import com.ylyq.ylxx.model.bean.SendQuestion;
import com.ylyq.ylxx.model.request.DeleteAnserRequest;
import com.ylyq.ylxx.service.IChatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaweichao
 * @since 2020-10-19
 */
@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements IChatService {

    @Autowired
    private ChatMapper chatMapper;

    @Override
    public SendQuestion sendQuestion(String question) {
        QueryWrapper<Chat> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.eq("question", question);
        chatQueryWrapper.orderByDesc("count");
        List<Chat> list = list(chatQueryWrapper);
        if (list == null || list.isEmpty()) {
            String[] ansers = new String[]{"听不懂你在说什么，能再说一遍吗", "不是很懂你的意思"};
            int index = (int) (Math.random() * ansers.length);
            String rand = ansers[index];
            List<String> anser = new ArrayList<>();
            anser.add(rand);
            return new SendQuestion(1, anser);
        } else {
            return new SendQuestion(0, list.stream().map(Chat::getAnser).collect(Collectors.toList()));
        }
    }

    @Override
    public void submitAnser(String question, String anser) {
        QueryWrapper<Chat> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.eq("question", question);
        chatQueryWrapper.eq("anser", anser);
        Chat one = getOne(chatQueryWrapper);
        if (one == null) {
            Chat chat = new Chat();
            chat.setAnser(anser);
            chat.setCount(1l);
            chat.setQuestion(question);
            save(chat);
        } else {
            one.setCount(one.getCount() + 1);
            updateById(one);
        }
    }

    @Override
    public void importChat(MultipartFile file) {
        try {
            Reader reader = new InputStreamReader(file.getInputStream(), "utf-8");
            BufferedReader br = new BufferedReader(reader);
            String line = null;
            StringBuilder result = new StringBuilder();
            while ((line = br.readLine()) != null) {
                // 一次读入一行数据
                if (line != null && line != "") {
                    result.append(line);
                }
            }
            reader.close();

            JSONObject jsonObject = JSONObject.parseObject(result.toString());
            ChatData chatData = JSONObject.toJavaObject(jsonObject, ChatData.class);
            if (chatData != null && chatData.getChatData() != null && chatData.getChatData().isEmpty()) {
                List<Chat> addChatList = new ArrayList<>();
                List<ChatData.ChatDataBean> chatDataBeans = chatData.getChatData();
                List<String> questionList = chatData.getChatData().stream().map(ChatData.ChatDataBean::getQuestion).collect(Collectors.toList());
                QueryWrapper<Chat> chatQueryWrapper = new QueryWrapper<>();
                chatQueryWrapper.in("question", questionList);
                chatQueryWrapper.select("question", "anser");
                List<Chat> chatList = list(chatQueryWrapper);
                for (Chat temp : chatList) {
                    ChatData.ChatDataBean chatDataBean = new ChatData.ChatDataBean(temp.getQuestion(), temp.getAnser());
                    if (chatDataBeans.contains(chatDataBean)) {
                        chatDataBeans.remove(chatDataBean);
                    }
                }

                for (ChatData.ChatDataBean temp : chatDataBeans) {
                    Chat chat = new Chat();
                    chat.setCount(1l);
                    chat.setQuestion(temp.getQuestion());
                    chat.setAnser(temp.getAnser());
                    addChatList.add(chat);
                }
                saveBatch(addChatList);

//                for (ChatData.ChatDataBean temp:chatData.getChatData()) {
//                    submitAnser(temp.getQuestion(),temp.getAnser());
//                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteAnser(DeleteAnserRequest deleteAnserRequest) {
        List<DeleteAnserRequest.ChatDataBean> chatData = deleteAnserRequest.getChatData();
        for (DeleteAnserRequest.ChatDataBean temp : chatData) {
            if(StringUtils.isNotBlank(temp.getQuestion())){
                if (temp.getAnser() == null || temp.getAnser().isEmpty()) {
                    QueryWrapper<Chat> chatQueryWrapper = new QueryWrapper<>();
                    chatQueryWrapper.eq("question", temp.getQuestion());
                    remove(chatQueryWrapper);
                }else {
                    QueryWrapper<Chat> chatQueryWrapper = new QueryWrapper<>();
                    chatQueryWrapper.eq("question", temp.getQuestion());
                    chatQueryWrapper.in("anser", temp.getAnser());
                    remove(chatQueryWrapper);
                }
            }
        }
    }

    @Override
    public int insertBatch(List<Chat> addChatList){
        return 0;
    }
}
